<template>
	<div>
		<div style="margin-bottom: 20px">
      <el-button type="primary" @click="templateAdd()"><i class="el-icon-plus"></i>公告</el-button>
    </div>
    <el-table v-loading="isLoading" :data="tableData" style="width: 100%" border>
      <el-table-column prop="announceId" label="公告ID" align="center" width="180"></el-table-column>
      <el-table-column prop="title" label="公告标题" align="center" width="180"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center" width="180"></el-table-column>
      <el-table-column prop="createUserName" label="创建人" align="center" show-overflow-tooltip  min-width="140"></el-table-column>
      <el-table-column prop="state" label="状态" align="center" width="80">
        <template scope="scope">
          <span>{{scope.row.state === 0 ? '已生效' : '已作废'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="140" fixed="right">
        <template scope="scope">
          <el-button @click="handle(scope.$index, scope.row, $event)" size="small">详情</el-button>
          <el-button v-if="scope.row.state === 0" @click="handle(scope.$index, scope.row, $event)" size="small">作废</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      @current-change="handleCurrentChange"
      :page-size="Number(pageSize)"
      layout="total, prev, pager, next"
      :total="Number(total)"
      style="padding-top:10px;text-align: right;">
    </el-pagination>
	</div>
</template>
<script>
  import { gzt } from '../../../config/proxy.js'
  // import { mapState } from 'vuex'
  import mixins from '../../../components/mixins.vue'
//  import { apiURL } from '../../config/settings.js'
  export default {
    mixins: [mixins],
    data () {
      return {
        pageSize: 10,
        total: '',
        page: 1,
        tableData: [],
        isLoading: false
      }
    },
    methods: {
      templateAdd () { // 新增
        this.openNewWindow('gonggaogl_add', '新增公告', 'gonggaogl_add')
      },
      searchList () { // 查询
        this.isLoading = true
        gzt.gonggaogl.list({page: this.page, pageSize: this.pageSize}, (res) => { // 查询
          // console.log(_data)
          if (res.message === 'ok') {
            this.isLoading = false
            this.tableData = res.content.content
            // console.log(this.tableData)
            this.total = res.content.count
          } else {
            this.isLoading = false
            this.message('查询失败', 'error')
          }
        })
      },
      handleCurrentChange (val) { // 分页
        this.page = val
        this.tableData = []
        this.isLoading = true
        this.searchList()
      },
      handle (index, row, event) { // 操作
        const handleName = event.target.innerText
        switch (handleName) {
          case '详情':
            this.openNewWindow('gonggaogl_detail', '公告详情', 'gonggaogl_detail', row.announceId)
            break
          case '作废':
            this.buttonPublicMethods('作废', 'invalid', row)
            break
        }
      },
      buttonPublicMethods (text, fnName, row) { // 删除 撤回 作废 公共方法
        this.$confirm('您确定要' + text + '吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          console.log(row)
          var _data = {
            announceId: row.announceId,
            updateUser: this.userId,
            updateUserName: this.userName
          }
          console.log(_data)
          gzt.gonggaogl[fnName](_data, res => {
            if (res.message === 'ok') {
              this.message('作废成功', 'success')
              this.$store.dispatch('gonggaoglState', this.$store.state.updateState.gonggaoglState + 1)
              this.searchList()
            } else {
              this.message(res.message, 'error')
            }
          })
        }).catch(() => {
        })
      },
      message (text, type) { // 消息提示
        this.$message({
          showClose: true,
          message: text,
          type: type
        })
      }
    },
    created () {
      this.searchList()
    },
    computed: {
      update_state: function () {
        // console.log(state.updateState.dhmState)
        return this.$store.state.updateState.gonggaoglState
      },
      userName: function () {
        if (!this.$store.state.user.info.user_name) {
          this.$store.dispatch('SET_TOKEN')
        }
        return this.$store.state.user.info.user_name
      },
      userId: function () {
        return this.$store.state.user.info.user_id
      }
    },
    watch: {
      update_state (curVal, oldVal) {  // 监听 更新状态 查询list
        this.searchList()
      }
    }
  }
</script>
<style>
.line{
	margin-left:20px;
	text-align:center
}
.el-button+.el-button{
  margin-left: 0
}
.el-table .cell, .el-table th>div{
  padding-left:10px;
  padding-right:10px;
}
.el-dialog--small{
  width:60%
}
</style>
